package com.lcifn.spring.transaction.manager;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.lcifn.spring.transaction.bean.User;
import com.lcifn.spring.transaction.mapper.UserJdbcTemplateMapper;
import com.lcifn.spring.transaction.mapper.UserMapper;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
public class UserManager {
	
	private UserMapper userMapper;
	
	private UserJdbcTemplateMapper jdbcMapper;

	public void batchOperator(){
		User user = new User("lily", 25);
		int insertRows = userMapper.insert(user);
		if(insertRows > 0){
			log.info(user.toString());
			user = userMapper.getUser(user.getId());
			log.info(user.toString());
			userMapper.delete(user.getId()-1);
			throw new IllegalArgumentException();
		}
	}
	
	@Transactional(timeout=1)
	public void batchUpdate(){
		List<User> userList = userMapper.getUserList();
		
		User user = new User("jucy", 24);
		user.setId(100L);
		userList.add(user);
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		int updateRow = userMapper.batchUpdate(userList);
		System.out.println("updateRow:" + updateRow);
	}
	
	@Transactional(timeout=1)
	public void batchUpdateWithJdbcTemplate(){
		List<User> userList = userMapper.getUserList();
		
		User user = new User("jucy", 24);
		user.setId(100L);
		userList.add(user);
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		int updateRow = jdbcMapper.updateUser();
		System.out.println("updateRow:" + updateRow);
	}
}
